/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.renderer;

import com.jb.web.bean.CheckBox;
import com.jb.web.context.WebContext;

import com.jb.faces.component.UIComponent;
import com.jb.faces.context.FacesContext;


public class CheckBoxRenderer extends FormItemRenderer {
    static final long serialVersionUID = 123123L;

    public void decode(FacesContext pCtx, UIComponent item) {
        CheckBox formField = (CheckBox)item;
        WebContext ctx = (WebContext)pCtx;
        String fieldName = formField.getFieldName(ctx);
        if (fieldName != null) {
            String value = ctx.getRequest().getParameter(fieldName);
            if (value != null) {
                boolean bValue = "true".equals(value) || "on".equals(value);
                formField.setDecodedValue(Boolean.valueOf(bValue));
            } else {
                formField.setDecodedValue(Boolean.valueOf(false));
            }
        }
    }

    public StringBuffer render(WebContext pCtx, UIComponent itemToRender) {
        CheckBox formField = (CheckBox)itemToRender;
        StringBuffer html = new StringBuffer("");

        String label = evaluate(pCtx, formField, formField.getLabel());
        Object value = formField.getModelBean();

        if (value == null) {
            if ((formField.getValue() == null) || 
                (formField.getValue().length() == 0)) {
                value = evaluate(pCtx, formField, formField.getDefaultValue());
            } else {
                value = evaluate(pCtx, formField, formField.getValue());
            }
        }

        value = (value == null) ? "false" : value.toString();

        boolean bValue = "true".equals(value) || "on".equals(value);

        String fieldName = formField.getFieldName(pCtx);
        html.append("<span ").append(addTagProperty("style", 
                                                    formField.getStyle(pCtx), 
                                                    null)).append(addTagProperty("styleClass", 
                                                                                 formField.getStyleClass(), 
                                                                                 null)).append(">").append("<INPUT name=\"").append(fieldName).append("\"").append(addTagProperty("type", 
                                                                                                                                                                                  "checkBox", 
                                                                                                                                                                                  "checkBox"));

        if (pCtx.isRunTime()) {
            html.append(addTagProperty(" disabled", 
                                       formField.isEnabled() ? "" : "disabled", 
                                       null));
        }

        html.append(getEditScript(pCtx, 
                                  formField)).append(addTagProperty("accessKey", 
                                                                    formField.getAccessKey(), 
                                                                    null));

        html.append(addTagProperty("size", formField.getWidth(), 
                                   null)).append(addTagProperty(" checked", 
                                                                (bValue ? 
                                                                 "checked" : 
                                                                 ""), null));

        html.append(" />" + label + "</span>\n").toString();

        return html;
    }
}
